package org.ioriframework.plugin.maven.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;

/**
 * 任务进程工具类
 * @author iori
 *
 */
public class ProcessUtils {

	/**
	 * 查找任务对应的进程ID列表
	 * @param processName 任务进程名称
	 * @return
	 * @throws IOException 
	 */
	public static Set<Integer> findPids(String processName) throws IOException {
		if (OSUtils.isWindows()) {
			return findPids4Windows(processName);
		}
		return findPids4Linux(processName);
	}
	
	/**
	 * 查找任务对应的进程ID列表(windows系统)
	 * @param processName 任务进程名称
	 * @return
	 * @throws IOException 
	 */
	public static Set<Integer> findPids4Windows(String processName) throws IOException {
		processName += ".exe";
		Set<Integer> pidSet = new HashSet<Integer>();
		BufferedReader br = null;
		try {
			Process proc = Runtime.getRuntime().exec("tasklist -fi " + "\"" + "imagename eq " + processName + "\" /FO LIST");
			br = new BufferedReader(new InputStreamReader(proc.getInputStream(), "GBK"));
			String line = null;
			while ((line = br.readLine()) != null) {
				if (line.startsWith("PID:")) {
					pidSet.add(Integer.valueOf(line.replaceAll("PID:", "").trim()));
				}
			}
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (Exception e) {
					
				}
			}
		}
		return pidSet;
	}
	
	/**
	 * 查找任务对应的进程ID列表(Linux系统)
	 * @param processName 任务进程名称
	 * @return
	 * @throws IOException 
	 */
	public static Set<Integer> findPids4Linux(String processName) throws IOException {
		Set<Integer> pidSet = new HashSet<Integer>();
		BufferedReader br = null;
		try {
			Process proc = Runtime.getRuntime().exec("pgrep " + processName);
			br = new BufferedReader(new InputStreamReader(proc.getInputStream(), "GBK"));
			String line = null;
			while ((line = br.readLine()) != null) {
				pidSet.add(Integer.valueOf(line.trim()));
			}
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (Exception e) {
					
				}
			}
		}
		return pidSet;
	}
	
	/**
	 * 杀掉指定PID的进程
	 * @param pid 进程ID
	 * @throws IOException
	 */
	public static void killByPid(Integer pid) throws IOException {
		if (OSUtils.isWindows()) {
			killByPid4Windows(pid);
		}
		killByPid4Linux(pid);
	}
	
	/**
	 * 杀掉指定PID的进程(windows系统)
	 * @param pid 进程ID
	 * @throws IOException
	 */
	public static void killByPid4Windows(Integer pid) throws IOException {
		if (pid != null) {
			BufferedReader br = null;
			try {
				Runtime.getRuntime().exec("taskkill /f /pid " + pid);
			} finally {
				if (br != null) {
					try {
						br.close();
					} catch (Exception e) {
						
					}
				}
			}
		}
	}
	
	/**
	 * 杀掉指定PID的进程(linux系统)
	 * @param pid 进程ID
	 * @throws IOException
	 */
	public static void killByPid4Linux(Integer pid) throws IOException {
		if (pid != null) {
			BufferedReader br = null;
			try {
				Runtime.getRuntime().exec("kill -9 " + pid);
			} finally {
				if (br != null) {
					try {
						br.close();
					} catch (Exception e) {
						
					}
				}
			}
		}
	}
	
}
